/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 14, 2012  3:17:55 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : UmMasterAction.java
 * TODO
 */
package apps.promis.in.common;

import java.util.List;
import jpa.entitys.promis.common.UmMaster;
import jpa.entitys.synchro.staging.MmConvFactInb;
import jpa.service.promis.common.UmMasterServices;
import jpa.service.synchro.staging.MmConvFactInbServices;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import apps.sessionFactory;

/***
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:37:56 PM 
 * CreateBy=Chatree Siangklom
 * 
 * UmMasterAction
 * TODO
 */
public class UmMasterAction {
	
	private static final Logger log = Logger.getLogger(UmMasterAction.class);
	
	public UmMaster FindByUmCode(String um_code) throws Exception 
	{
		UmMasterServices  cc = new UmMasterServices(sessionFactory.CreateEmPromis());
		return cc.findById(CommonService.IsNullRString(um_code));
	}
	
	public boolean ExistingChecked(String um_code) throws Exception
	{
		return FindByUmCode(um_code) != null ? true : false;
	}

	public UmMaster InsertOrUpdatePromisUmMaster(MmConvFactInb mmConvFactInb) throws Exception {
		try{

			UmMasterServices umMasterSrv = new UmMasterServices(sessionFactory.CreateEmPromis());
			UmMaster model = umMasterSrv.findById(mmConvFactInb.getUm());
					
			if(model == null){
				
				model = new UmMaster();
				model.setUm(mmConvFactInb.getUm());				
				model.setDescription(mmConvFactInb.getUmDesciption());
				
				model.setSyncExternalRefKey(mmConvFactInb.getExtRefKey());
				model.setSyncExternalRefNo(mmConvFactInb.getExtRefNo());
				
				model.setSyncIssync("N");
				model.setCreatedBy(mmConvFactInb.getCreatedBy());
				model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmConvFactInb.getCreatedOn()));
				
			}else
			{			
				model.setUm(mmConvFactInb.getUm());
				model.setDescription(mmConvFactInb.getUmDesciption());
				
				model.setSyncExternalRefKey(mmConvFactInb.getExtRefKey());
				model.setSyncExternalRefNo(mmConvFactInb.getExtRefNo());
				
				model.setSyncIssync("N");
				if(CommonService.IsNull(mmConvFactInb.getUpdatedOn()))
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmConvFactInb.getCreatedOn()));				
				else
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmConvFactInb.getUpdatedOn()));
				
				model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmConvFactInb.getUpdatedOn()));
				
			}
			
			return umMasterSrv.InsertedOrUpdated(model);
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:38:08 PM 
	 * CreateBy=Chatree Siangklom
	 * List<MmConvFactInb>
	 * TODO
	 * @param lst
	 * @return
	 * @throws Exception
	 */
	public List<MmConvFactInb> CreateToPromis(List<MmConvFactInb> lst) throws Exception {
		
		try{
			
			MmConvFactInbServices objMmConvFactInbSrv = new MmConvFactInbServices(sessionFactory.CreateEmSynchro());
			
			if(!CommonService.IsNull(lst)){
				for(MmConvFactInb mmConvFactInb : lst)
				{
					try{
						
						InsertOrUpdatePromisUmMaster(mmConvFactInb);
						
						mmConvFactInb.setIssync("Y");
						mmConvFactInb.setMsgCode("S");
						mmConvFactInb.setMsgDesc("Success");
						mmConvFactInb.setMsgActResult(null);
						objMmConvFactInbSrv.Updated(mmConvFactInb);
					}catch(Exception ex)
					{
						mmConvFactInb.setIssync("N");
						mmConvFactInb.setMsgCode("E");
						mmConvFactInb.setMsgDesc("Error");
						mmConvFactInb.setMsgActResult(ex.getMessage());
						objMmConvFactInbSrv.Updated(mmConvFactInb);
						log.error(ex);
					}			
				}
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return null;
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:38:11 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	@Deprecated
	private void Tested() throws Exception {
		
		MmConvFactInbServices objMmConvFactInbSrv = new MmConvFactInbServices(sessionFactory.CreateEmSynchro());
		List<MmConvFactInb> lst = objMmConvFactInbSrv.findByIsSync("N");
		
		if(!CommonService.IsNull(lst)){
			for(MmConvFactInb mmConvFactInb : lst)
			{
				try{
					
					InsertOrUpdatePromisUmMaster(mmConvFactInb);
					
					mmConvFactInb.setIssync("Y");
					mmConvFactInb.setMsgCode("S");
					mmConvFactInb.setMsgDesc("Success");
					mmConvFactInb.setMsgActResult(null);
					objMmConvFactInbSrv.Updated(mmConvFactInb);
				}catch(Exception ex)
				{
					mmConvFactInb.setIssync("N");
					mmConvFactInb.setMsgCode("E");
					mmConvFactInb.setMsgDesc("Error");
					mmConvFactInb.setMsgActResult(ex.getMessage());
					objMmConvFactInbSrv.Updated(mmConvFactInb);
					System.out.println(ex.getMessage());
					log.error(ex.getMessage());
				}			
			}
		}
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:38:15 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param args
	 */
	public static void main(String [] args){
		
		UmMasterAction xx = new UmMasterAction();
		try {
			
			xx.Tested();
			
		} catch (Exception e) {
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
}
